package xupt.se.ttms.view.employee;

import java.awt.Color;
import java.awt.Image;
import java.awt.Insets;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import xupt.se.ttms.model.Customer;
import xupt.se.ttms.model.Employee;
import xupt.se.ttms.service.CustomerSrv;
import xupt.se.ttms.service.EmployeeSrv;
import xupt.se.ttms.view.tmpl.Login;




public class Employee_ChangePassWord {
	public static JFrame frame;
	public static JTextField b2;
	public static JTextField c2;
	public static JTextField d2;
	public static JTextField e2;
	public  Employee_ChangePassWord(String access)
	{
		frame = new JFrame();
		frame.setSize(850,500);
		frame.setTitle("用户修改密码界面");
		frame.setBounds(0, 0, 850, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		ImageIcon icon=new ImageIcon("img/2.png");
		JLabel x = new JLabel(icon);
		icon.setImage(icon.getImage().getScaledInstance(850,70,Image.SCALE_DEFAULT));
		JPanel a=new JPanel();
		a.setBackground(Color.decode("#FFe4c4"));
		a.setLayout(null);
		x.setBounds(0,0,850,70);
		a.add(x);
		frame.add(a);
		JLabel a1=new JLabel("密码修改");
		a1.setBounds(400,80,100,25);
		a.add(a1);
		JLabel b1=new JLabel("账号:");
		b1.setBounds(300,130,50,25);
		
		a.add(b1);
		b2=new JTextField();
		b2.setBounds(350,130,200,25);
		b2.setText(access);
		b2.setEditable(false);
		a.add(b2);
		JLabel c1=new JLabel("旧密码:");
		c1.setBounds(300,160,75,25);
		a.add(c1);
		c2 = new JPasswordField();
		c2.setBounds(350,160,200,25);
		a.add(c2);
		JLabel d1=new JLabel("新密码：");
		d1.setBounds(300,190,75,25);
		a.add(d1);
		d2 = new JPasswordField();
		d2.setBounds(350,190,200,25);
		a.add(d2);
		JLabel e1=new JLabel("确认：");
		e1.setBounds(300,220,50,25);
		a.add(e1);
		 e2 = new JPasswordField();
		e2.setBounds(350,220,200,25);
		a.add(e2);
		
		JButton enter=new JButton("确认修改");
		enter.setBounds(350,290,90,30);
		enter.setFocusPainted(false);
		enter.setMargin(new Insets(0,0,0,0));
		enter.setContentAreaFilled(false);
		enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				String access =b2.getText();
				String password=c2.getText();
				String new_password=d2.getText();
				String again_password=e2.getText();
				if(access=="" ||password==""||new_password==""||again_password=="")
					JOptionPane.showConfirmDialog(null, "输入不能为空");
				else if (!new_password.equals(again_password)) {
					JOptionPane.showConfirmDialog(null, "两次密码输入不一致");
				}
				else 
				{
					Employee employee=new Employee();
					EmployeeSrv employeeSrv=new EmployeeSrv();
					List<Employee> list=new ArrayList<Employee>();
					try {
						list=employeeSrv.Fetch(access);
						if(list.size()==0)
						{
							JOptionPane.showConfirmDialog(null, "没有该账号");
						}
						else
						{
							if(list.get(0).getPassword().equals(password))
							{
								employee.setAccess(list.get(0).getAccess());
								employee.setName(list.get(0).getName());
								employee.setcName(list.get(0).getcName());
								employee.setTel(list.get(0).getTel());
								employee.setPassword(new_password);
								employee.setId(list.get(0).getId());
								
								employeeSrv.modify(employee);
								frame.dispose();
								Login login=new Login();
							}
							else
							{
								JOptionPane.showConfirmDialog(null, "原始密码输入错误");
							}
						}
					} catch (Exception e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}							
			}
		});
		a.add(enter);
		JButton enter1=new JButton("返回");
		enter1.setBounds(460,290,90,30);
		enter1.setFocusPainted(false);
		enter1.setMargin(new Insets(0,0,0,0));
		enter1.setContentAreaFilled(false);
		enter1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				frame.dispose();
				Employee_MainUITmpl customer_MainUITmpl=new Employee_MainUITmpl(access);
			}
		});
		a.add(enter1);
		frame.setVisible(true);
		frame.setLayout(null);
		
		
	}

}
